<script>
  jQuery(document).ready(function(){
    create_header('#player_stats', "500px;");
    create_header('#player_history');
    create_header('#compare_players');
    jQuery('#team_id').change();
    
    show_table_partial(".table_stat");
    jQuery(".table_stat a").click();
    jQuery(".tablesorter").tablesorter({sortList: [[0,0],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1],[8,1],[9,1],[10,1],[11,1],[12,1],[13,1]]}); 
  });
</script>

<form action="index.php" method="post" name="adminForm">
  <div id="tabs" style="padding-top:10px;">
    <ul>
      <? $letters = array('A', 'B' , 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); ?>
      <? foreach($letters as $letter): ?>
        <li><a href="<?= JURI::base(true) ?>/component/pbny/players#players-<?= $letter ?>"><?= $letter ?></a></li>      
      <? endforeach; ?>                
    </ul>
  </div>
  <? 
    $pic       = null;
    $feature_pic = null;
    $person   = $this->player->person;
    $position = $this->player->position; 
    $team     = $this->teams[$this->player->team_id];
    $title    = "$person->lastname, $person->firstname | #".
          $this->player->jersey_number." | ".
          $position->position;
          
    if(isset($this->player->pic))
      $pic          = $this->player->pic;
    if(isset($this->player->feature_pic))
      $feature_pic  = $this->player->feature_pic;

  ?>
  <div style="border-bottom:solid 1px #454545; margin:5px 10px 10px;padding:0px 10px 5px;">
    <h2 style="margin:0px; width:100%;"><?= $title ?><span style="float:right; margin-right:60px;font-size:1.2em"><?= $team->abbr ?></span></h2>
  </div>
  
  <div style="float:left; border:solid 1px #9D9D9D; padding:2px; width:200px; margin-left:10px;">
    <img src="<?= PBNYHelper::getFlickrImageUrl($pic->flickr_id, 'small') ?>" width="200px" height="300px" alt="no image"/>
  </div>
  
  <div style="float:right; border:solid 1px #9D9D9D; padding:2px; width:200px; margin-right:10px;">
    <img src="<?= PBNYHelper::getFlickrImageUrl($feature_pic->flickr_id, 'small') ?>" width="200px" height="300px" alt="no image" />
  </div>

  <? $cols = array();
     $cols['PPG'] = 'avg_points';
     $cols['FGPG'] = 'avg_field_goals';
     $cols['FTPG'] = 'avg_free_throws';
     $cols['3PPG'] = 'avg_three_points';
     $cols['ORPG'] = 'avg_off_rebounds';
     $cols['DRPG'] = 'avg_def_rebounds';
     $cols['APG'] = 'avg_assists';
     $cols['SPG'] = 'avg_steals';
     $cols['BPG'] = 'avg_blocks';
  ?>
  
  <div id="player_stats" style="padding-top:10px;">
    <h3>Player Stats</h3>
    <? if(!empty($this->player->avg_stats)): ?>
      <? foreach($this->player->avg_stats as $s_id => $avg_stat): ?>
      <div style="padding:5px;font-weight:bold;"><?= $this->seasons[$s_id] ?></div>
      <table style="width:450px; margin:auto;">
        <tbody>          
          <? foreach($cols as $key=>$value){ ?>              
            <tr>
              <td style="background-color:#111; width:200px;"><?= $key ?></td>
              <td style="background-color:#333;"><?= $avg_stat[$value] ?></td>
            </tr>
          <? } ?>
        </tbody>
      </table>
      <?  endforeach; ?>      
    <? else: ?>
      <div style="line-height:300px;">
        <h2>No player stats found.</h2>
      </div>
    <? endif; ?>
  </div>        

  <div id="player_history">
    <h3>Player History</h3>
    <? if(isset($this->player->stats)): 
      foreach($this->player->stats as $s_id => $stats): ?>
      
      <div class="table_stat">
      <div style="padding:5px;font-weight:bold;"><?= $this->seasons[$s_id] ?></div>
      <table style="width:100%; margin:20px 0px;" class="tablesorter">
        <thead>
          <tr>
            <th class="cell_key">OPPONENT TEAM</th>
            <th class="cell_key">SCHEDULE</th>
            <th class="cell_key">POINTS</th>
          
            <th class="cell_key">FG</th>
            <th class="cell_key">FGA</th>
            
            <th class="cell_key">TP</th>
            <th class="cell_key">TPA</th>
            
            <th class="cell_key">FT</th>
            <th class="cell_key">FTA</th>
            
            <th class="cell_key">OFF REB</th>
            <th class="cell_key">DEF REB</th>
            
            <th class="cell_key">ASSISTS</th>
            <th class="cell_key">STEALS</th>
            <th class="cell_key">BLOCKS</th>
            
          </tr>
        </thead>
        <tbody>
      
    <?    foreach($stats as $s_id => $stat): ?>
          <tr>
            <td class="cell_value" align="left" style="padding-left:20px"><?= $this->teams[$stat->opponent_team_id]->name ?></td>
            <td class="cell_value"><?= $stat->schedule ?></td>
            
            <td class="cell_value"><?= $stat->points ?></td>
            <td class="cell_value"><?= $stat->field_goals ?></td>
            <td class="cell_value"><?= $stat->field_goal_attempts ?></td>
            
            <td class="cell_value"><?= $stat->three_points ?></td>
            <td class="cell_value"><?= $stat->three_point_attempts ?></td>
            <td class="cell_value"><?= $stat->free_throws ?></td>
            <td class="cell_value"><?= $stat->free_throw_attempts ?></td>
            
            <td class="cell_value"><?= $stat->off_rebounds ?></td>
            <td class="cell_value"><?= $stat->def_rebounds ?></td>
            
            <td class="cell_value"><?= $stat->assists ?></td>
            <td class="cell_value"><?= $stat->steals ?></td>
            <td class="cell_value"><?= $stat->blocks ?></td>
            
          </tr>
    <?    endforeach; ?>
        </tbody>
      </table>
      <? if(count($stats) > 6): ?>
        <a href="" style="margin-left:900px;margin-bottom:10px">Show All</a>
      <? endif; ?>
    <?  endforeach; ?>
    <? else: ?>
      <div style="line-height:80px;">
        <h2>No player history found.</h2>
      </div>
    <? endif; ?>
    </div>
  </div>
  
  <div id="compare_players" style="padding-top:10px;">
    <h3>Compare with:</h3>
    <div>
      <? $team_link = "onchange=\"ajx_request('players','ajx_select_players','team_id='+this.value, 'span_players')\""?>
      <p>Select Team: <?= JHTML::_('select.genericlist', $this->teams, 'team_id', $team_link, 'id', 'name', null);?></p>
      <p>
        Select Player: <span id="span_players"><select></select></span>
        <? $link = JURI::base(true)."/component/pbny/players/compare_players/".$this->player->id; ?>
        <input type="button" onclick="javascript:redirect('<?= $link ?>', jQuery('#player_id').val());" value="Compare">
      </p>
    </div>
  </div>
</form>
